package com.example.mentalhealth.Controller.UserController;


import com.example.mentalhealth.Service.UserService;
import com.example.mentalhealth.entities.ResultData;
import com.example.mentalhealth.entities.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;


@RestController
@RequestMapping("/api")
public class ProfileController {
    @Autowired
    public UserService userService;


    /**
     * 获取用户信息
     * @param username
     * @return
     */
    @RequestMapping("/getUserInfo")
    public ResultData getUserInfo(@RequestParam(required = false) String username) {
        System.out.println("getUserInfo: " + username);
        String user = username.replace("\"", "");
        return new ResultData(200, "获取用户信息成功", userService.queryUserByUsername(user));
    }
    /**
     * 更新用户信息
     * @return
     */
    @RequestMapping("/updateUserInfo")
    public ResultData updateUserInfo(@RequestBody User  user){
        System.out.println("updateUserInfo: " + user);
        int result = userService.updateUserByUId(user.getUId(), user);
        if (result <= 0) {
            return new ResultData(500, "更新用户信息失败");
        }
        return new ResultData(200, "更新用户信息成功");
    }


    @PostMapping("/uploadUrl")
    public ResultData uploadAvatar(@RequestParam("file") MultipartFile file, HttpServletRequest  request) {
        if (file.isEmpty()) {
            return new ResultData(400, "文件为空");
        }

        // 获取文件名
        String fileName = file.getOriginalFilename();
        // 获取文件的后缀名
        assert fileName != null;
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        // 生成新的文件名，使用UUID防止文件重名
        fileName = UUID.randomUUID() + suffixName;

        // 设置文件存储路径，这里存储在项目的static/images目录下
        String filePath = System.getProperty("user.dir") + "/src/main/resources/static/images/";
        File dest = new File(filePath + fileName);

        // 检查文件目录是否存在，如果不存在则创建
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }

        try {
            // 保存文件
            file.transferTo(dest);
            System.out.println("文件保存成功：" + filePath + fileName);

            // 构建可访问的URL（使用服务器上下文路径）
            String serverPath = request.getScheme() + "://" +
                    request.getServerName() + ":" +
                    request.getServerPort() +
                    request.getContextPath() +
                    "/images/" + fileName;
            // 返回文件的访问路径
            System.out.println("serverPath: " + serverPath);
            return new ResultData(200, "上传成功", serverPath);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResultData(500, "上传失败");
        }
    }
    @GetMapping("/test")
    public String test() {
        return "服务正常运行！当前时间：" + new java.util.Date();
    }


}
